package cn.sc.summer.warm.endpoint;

import cn.sc.summer.constant.model.Result;
import cn.sc.summer.constant.project.ProjectConstant;
import cn.sc.summer.redis.util.NacosInstancesService;
import cn.sc.summer.redis.util.RedisHelper;
import cn.sc.summer.warm.service.WarmUpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 类名：接口预热
 *
 * @author a-xin
 * @date 16:38
 */
@Slf4j
@RestController
public class WarmupEndpoint {

    @Resource
    private WarmUpService warmUpService;

    public final static String WARM_UP = "WARM_UP:";

    public Result<String> warmup() {
        String serverName = ProjectConstant.SERVER_NAME;
        log.debug("-> The {} warm up request in...", serverName);
        String redisKey = WARM_UP + serverName + ":" + NacosInstancesService.getServerNacosAddress();
        RedisHelper.incr(redisKey, 1L);

        if (Integer.parseInt(String.valueOf(RedisHelper.get(redisKey))) > 10) {
            warmUpService.unRegister();
        }

        return Result.success();
    }

}
